package com.zjw.spring_ai;

import com.zjw.spring_ai.utils.FileUtils;
import jakarta.annotation.Resource;
import org.springframework.ai.openai.OpenAiAudioSpeechModel;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TranscriptionController {

    @Resource
    private OpenAiAudioTranscriptionModel openAiAudioTranscriptionModel;

    @Resource
    private OpenAiAudioSpeechModel openAiAudioSpeechModel;

    @RequestMapping(value = "/ai/transcription")
    public Object transcription() {
        org.springframework.core.io.Resource audioFile = new ClassPathResource("cat.mp3");
        String called = openAiAudioTranscriptionModel.call(audioFile);
        System.out.println(called);

        return called;
    }


    @RequestMapping(value = "/ai/tts")
    public Object tts() {
        String text = "2023年全球汽车销量重回9000万辆大关，同比2022年增长11%。分区域看，西欧（14%）、中国（12%）两大市场均实现两位数增长。面对这样亮眼的数据，全球汽车行业却都对2024年的市场前景表示悲观，宏观数据和企业体感之前的差异并非中国独有，在汽车市场中，这是共性问题。";
        byte[] bytes = openAiAudioSpeechModel.call(text);
        FileUtils.save2File("D:\\SpringAI\\test.mp3", bytes);
        return "OK";
    }

}

